<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div>
        <form action="/goodsEdit" method="post">
            <input type="hidden" name="_id" class="I1">
            商品名称:<input type="text" name="goodsName" class="I1"><br>
            商品介绍:<input type="text" name="goodsDetails" class="I1"><br>
            商品单价:<input type="text" name="price" class="I1"><br>
            商品数量:<input type="text" name="number" class="I1"><br>
            <input type="submit" value="修改" class="I2">
        </form>
    </div>
<!-- <script src="/js/edit.js" type="module"></script> -->
    <script>
        let connect = document.querySelectorAll('.I1');
        let btn = document.querySelector('.I2');
        const x = new XMLHttpRequest();
        x.open('get',`http://localhost:8000/getAppoint?id=${getQueryVariable("id")}`);
        x.send();
        x.onreadystatechange =()=>{
            if(x.readyState===4&&x.status===200){
                // console.log(x.response);
                const data = JSON.parse(x.response);
                const {_id,goodsName,goodsDetails,price,number}=data;
                connect[0].value = _id;
                connect[1].value=goodsName;
                connect[2].value=goodsDetails;
                connect[3].value=price;
                connect[4].value=number;
            }
        }
        console.log(getQueryVariable("id"));
        function getQueryVariable(variable) {
            //获取当前URL中的查询字符串部分（即问号后面的部分）
            var query = window.location.search.substring(1);
            //把查询字符串按照&符号分割成数组
            var vars = query.split("&");
            //遍历数组中的每个元素
            for (var i = 0; i < vars.length; i++) {
                //把当前元素按照=符号分割成键和值
                var pair = vars[i].split("=");
                //如果当前元素的键等于参数名，则返回对应的值
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
            //如果没有找到匹配的参数名，则返回false
            return false;
        }
    </script>
</body>

</html>